#include "MouseInput.h"

Event MouseInput::MOUSE_UP("UP");
Event MouseInput::MOUSE_DOWN("DOWN");
Event MouseInput::MOUSE_CLICK("CLICK");
Event MouseInput::RIGHT_MOUSE_UP("R_UP");
Event MouseInput::RIGHT_MOUSE_DOWN("R_DOWN");
Event MouseInput::RIGHT_MOUSE_CLICK("R_CLICK");

MouseInput::MouseInput(void):
rCounter(0), lCounter(0), mouseX(0), mouseY(0), oldB(0), clickDelay(Config::FPS * 2)
{
}

MouseInput::~MouseInput(void)
{
}

void MouseInput::update(Event *e)
{
	mouseX = mouse_x;
	mouseY = mouse_y;

	if( mouse_b & 1 )
	{
		if( !(oldB & 1) )
			dispatchEvent(&MouseInput::MOUSE_DOWN); //left mouse button pressed
		else
			++lCounter; //left mouse button held down
	}
	else
	{
		if( (oldB & 1) )
			dispatchEvent(&MouseInput::MOUSE_UP); //left mouse button released
		if(lCounter < clickDelay)
			dispatchEvent(&MouseInput::MOUSE_CLICK); //left mouse button click
		lCounter = 0;
	}

	if( mouse_b & 2 )
	{
		if( !(oldB & 2) )
			dispatchEvent(&MouseInput::RIGHT_MOUSE_DOWN); //right mouse button pressed
		else
			++rCounter; //right mouse button held down
	}
	else
	{
		if( (oldB & 2) )
			dispatchEvent(&MouseInput::RIGHT_MOUSE_UP); //right mouse button released
		if(rCounter < clickDelay)
			dispatchEvent(&MouseInput::RIGHT_MOUSE_CLICK); //right mouse button click
		rCounter = 0;
	}

	oldB = mouse_b;
}
